from django.db import models
from django.db.models.signals import pre_delete  # 删除文件
from django.dispatch.dispatcher import receiver  # 删除文件


# Create your models here.
# 封面表
class Cover(models.Model):
    nid = models.AutoField(primary_key=True)
    url = models.FileField(verbose_name='封面地址', upload_to='cover/')
    create_date = models.DateTimeField(verbose_name='上传时间', auto_now_add=True)
    state_choices = (
        (0, '复制粘贴'),
        (1, '直接上传'),
        (2, '粘贴地址'),
    )
    state = models.IntegerField(verbose_name='上传状态的类型', default=1, choices=state_choices)

    def __str__(self):
        return str(self.url)

    class Meta:
        verbose_name_plural = '封面'


@receiver(pre_delete, sender=Cover)  # sender=你要删除或修改文件字段所在的类**
def download_delete(instance, **kwargs):  # 函数名随意
    # print('进入文件删除方法，删的是', instance.url)  # 用于测试
    instance.url.delete(False)  # file是保存文件或图片的字段名**
